package com.example.zz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zz.entity.Attestation;
import com.example.zz.entity.Student;
import com.example.zz.entity.Teacher;
import com.example.zz.entity.User;
import com.example.zz.mapper.AttestationMapper;
import com.example.zz.mapper.StudentMapper;
import com.example.zz.mapper.TeacherMapper;
import com.example.zz.mapper.UserMapper;
import com.example.zz.service.IAttestationService;
import com.example.zz.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户认证 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Service
public class AttestationServiceImpl extends ServiceImpl<AttestationMapper, Attestation> implements IAttestationService {

    private static PageVO<Attestation> pageVO=new PageVO<>();

    @Autowired
    AttestationMapper attestationMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    StudentMapper studentMapper;


    /**
     * 角色分页查询用户认证信息实现类
     * @param page
     * @param role
     * @return
     */
    @Override
    public PageVO<Attestation> seletAttestationPageByRole(Page page, Integer role) {
        IPage iPage=attestationMapper.selectPage(page, Wrappers.<Attestation>lambdaQuery().eq(Attestation::getRole,role));

        if (iPage.getRecords().size()!=0) {
            pageVO.setCode(1);
            pageVO.setData(iPage.getRecords());
            pageVO.setCount(iPage.getTotal());
        }
        else pageVO.setCode(0);


        pageVO.setMsg("没有数据");
        return pageVO;
    }

    /**
     * 设置审核结果实体类
     * @param result
     * @return
     */
    @Override
    public int updateAttestationResult(Long id,Integer role,Integer result) {
        Attestation attestation=attestationMapper.selectById(id);

        attestation.setId(id);
        attestation.setResult(result);
        int an=attestationMapper.updateById(attestation);

        if (role==1){
            int t=0;
            if (result==0){
                teacherMapper.deleteById(id);
            }
            if (result==2){
                User user=userMapper.selectById(id);
                Teacher teacher=new Teacher();
                teacher.setId(id);
                teacher.setTeaName(user.getName());
                teacher.setCollegeId(attestation.getCollegeId());
                t=teacherMapper.insert(teacher);
            }
            if (an==1||(t==1&&an==1)){
                return an;
            }
        }
        if (role==0){
            int s=0;
            if (result==0){
                studentMapper.deleteById(id);
            }
            if (result==2){
                User user=userMapper.selectById(id);
                Student teacher=new Student();
                teacher.setId(id);
                teacher.setStuName(user.getName());
                teacher.setClassId(attestation.getCollegeId());
                s=studentMapper.insert(teacher);
            }
            if (an==1||(s==1&&an==1)){
                return an;
            }
        }

        return -1;
    }

    /**
     * 按照审核状体查找用户认证信息实体类
     * @param page
     * @param result
     * @return
     */
    @Override
    public PageVO<Attestation> seletAttestationByResult(Page page,Integer role, Integer result) {
        IPage iPage=attestationMapper.selectPage(page, Wrappers.<Attestation>lambdaQuery().eq(Attestation::getResult,result));

        if (iPage.getTotal()==0) pageVO.setCode(1);
        else pageVO.setCode(0);

        pageVO.setData(iPage.getRecords());
        pageVO.setCount(iPage.getTotal());
        pageVO.setMsg("没有数据");

        return pageVO;
    }
}
